//
//  MYXRechargeViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/10.
//  Copyright © 2017年 hbjApple. All rights reserved.
//充值界面

#import "MYXRechargeViewController.h"
#import "MYY_walletNavView.h"
#import "M80AttributedLabel.h"
#import "MYXRechargeCell.h"
#import "MYXRechargeTopView.h"
#import "MYXRechargeCenterView.h"
#import "MYXAuthenticationBottomView.h"

static NSString *const MYXRechargeCellIdentifier = @"MYXRechargeCell";
static NSString *const MYXRechargeTopViewIdentifier = @"MYXRechargeTopView";
static NSString *const MYXRechargeCenterViewIdentifier = @"MYXRechargeCenterView";
static NSString *const MYXAuthenticationBottomViewIdentifier = @"MYXAuthenticationBottomView";

@interface MYXRechargeViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,M80AttributedLabelDelegate>
{
    MYXRechargeCell *MYXRechargeZFBCell;
    MYXRechargeCell *MYXRechargeWXCell;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)MYY_walletNavView *MYY_walletNavVi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MYXRechargeTopView *MYXRechargeTopVi;
@property (nonatomic, strong)MYXRechargeCenterView *MYXRechargeCenterV;
@property(nonatomic, strong) M80AttributedLabel *AgreementLabel;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property(nonatomic, strong)MYXAuthenticationBottomView *MYXAuthenticationBottomVi;
@end

@implementation MYXRechargeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

//导航栏
-(void)initNavgationBar
{
    
    WS(weakSelf)
    self.MYY_walletNavVi =[[MYY_walletNavView alloc]init];
    self.MYY_walletNavVi.frame = CGRectMake(0, 0, _tableView.frame.size.width,SafeAreaTopHeight);
    [self.view addSubview:self.MYY_walletNavVi];
    self.MYY_walletNavVi.backButton =^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    };
    self.MYY_walletNavVi.rightBtn.hidden = YES;
    self.MYY_walletNavVi.nameLable.text = @"充值";
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    AdjustsScrollViewInsetNever(self, self.tableView);
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self createUI];
}

#pragma mark - UI

- (void)createUI
{
    
    WS(weakSelf)
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MYXRechargeCellIdentifier bundle:nil] forCellReuseIdentifier:MYXRechargeCellIdentifier];
    self.MYXRechargeTopVi    =[[NSBundle mainBundle]loadNibNamed:MYXRechargeTopViewIdentifier owner:self options:nil][0];
    self.MYXRechargeTopVi.frame = CGRectMake(0, 0, _tableView.frame.size.width,264);
    self.tableView.tableHeaderView =self.MYXRechargeTopVi;
    [self initNavgationBar];
    self.AgreementLabel.frame = CGRectMake(0, 0, _tableView.frame.size.width,50);
    self.tableView.tableFooterView = self.AgreementLabel;
    
}


- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MYXRechargeZFBCell = (MYXRechargeCell *)[tableView dequeueReusableCellWithIdentifier:MYXRechargeCellIdentifier];
        MYXRechargeZFBCell.selectionStyle = UITableViewCellSelectionStyleNone;
        MYXRechargeZFBCell.indexPath = indexPath;
        [MYXRechargeZFBCell.selectBtn addTarget:self action:@selector(zfbBtn) forControlEvents:UIControlEventTouchUpInside];
        return MYXRechargeZFBCell;
    }else{
        MYXRechargeWXCell = (MYXRechargeCell *)[tableView dequeueReusableCellWithIdentifier:MYXRechargeCellIdentifier];
        MYXRechargeWXCell.selectionStyle = UITableViewCellSelectionStyleNone;
        MYXRechargeWXCell.indexPath = indexPath;
        [MYXRechargeWXCell.selectBtn addTarget:self action:@selector(wxBtn) forControlEvents:UIControlEventTouchUpInside];
        return MYXRechargeWXCell;
    }
 
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   // WS(weakSelf)
    self.MYXRechargeCenterV   =[[NSBundle mainBundle]loadNibNamed:MYXRechargeCenterViewIdentifier owner:self options:nil][0];
    self.MYXRechargeCenterV.frame = CGRectMake(0, 0, WIDTH,41);
   
    return self.MYXRechargeCenterV;
}
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 41;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    self.MYXAuthenticationBottomVi                                =[[NSBundle mainBundle]loadNibNamed:MYXAuthenticationBottomViewIdentifier owner:self options:nil][0];
    self.MYXAuthenticationBottomVi.frame = CGRectMake(0, 0, WIDTH,60);
    [self.MYXAuthenticationBottomVi.sureButton setTitle:@"立即充值" forState:UIControlStateNormal];
    WS(weakSelf);

    weakSelf.MYXAuthenticationBottomVi.sureB= ^{
            NSString *moneyNum = [NSString stringWithFormat:@"%.01f",self.MYXRechargeTopVi.moneyTF.text.floatValue];
        if (moneyNum.floatValue < 0.1) {
            ShowSVProgressErrorMessage(@"请填写正确的充值金额！！");
          //  [SVProgressHUD showErrorWithStatus:@"请填写正确的充值金额！！"];
            return ;
        }
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        [[MYXHttpManager shared] rechargeWithPayType:@"alipay" money:moneyNum result:^(BOOL sucess, NSString *msg) {
                [SVProgressHUD showSuccessWithStatus:msg];
            if (sucess) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        } errorResult:^(NSError *enginerError) {
            ShowSVProgressErrorMessage(@"充值接口出错！！");
           // [SVProgressHUD showErrorWithStatus:@"充值接口出错！！"];

        }];
    };
    return self.MYXAuthenticationBottomVi;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 60;
}


- (M80AttributedLabel *)AgreementLabel
{
    if (_AgreementLabel == nil) {
        _AgreementLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _AgreementLabel.font = [UIFont systemFontOfSize:11];
        _AgreementLabel.textColor =[UIColor grayColor];
        _AgreementLabel.textAlignment = kCTTextAlignmentCenter;
        _AgreementLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];//MYYheadColor
        _AgreementLabel.numberOfLines = 1;
        _AgreementLabel.delegate = self;
        _AgreementLabel.underLineForLink = NO;
       // [_AgreementLabel setText:@"点击立即充值，即表示您已经同意  充值协议"];
        [_AgreementLabel addCustomLink:@"Agreement" forRange:NSMakeRange(17, 4) linkColor: MYYColorFromRGB(0xea4f48)];
    }
    return _AgreementLabel;
}

#pragma mark - M80Attribute delegate
- (void)m80AttributedLabel:(M80AttributedLabel *)label
             clickedOnLink:(id)linkData
{
    if ([linkData isEqualToString:@"Agreement"]) {
      
    }
    
}
#pragma mark - PrivateMethod
- (void)zfbBtn{
    MYXRechargeZFBCell.selectBtn.selected = !MYXRechargeZFBCell.selectBtn.selected;
    MYXRechargeWXCell.selectBtn.selected = !MYXRechargeZFBCell.selectBtn.selected;
}
- (void)wxBtn{
    MYXRechargeWXCell.selectBtn.selected = ! MYXRechargeWXCell.selectBtn.selected;
    MYXRechargeZFBCell.selectBtn.selected = ! MYXRechargeWXCell.selectBtn.selected;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
